package com.byx.imclientutil.utils;

import com.byx.imclientutil.entity.BaseMessage;
import com.byx.imclientutil.protobuf.ChatMessageProtobuf;

/**
 * BaseMessageConverter
 * 提供 {@link ChatMessageProtobuf.Msg}类型消息与{@link BaseMessage}类型的转换
 * @author Superb
 * @date 2021/4/5 10:37
 */
public class BaseMessageConverter {
    public static ChatMessageProtobuf.Msg toMsg(BaseMessage baseMessage){
        ChatMessageProtobuf.Msg.Builder builder = ChatMessageProtobuf.Msg.newBuilder();
        if (!baseMessage.getId().isEmpty()){
            builder.setMessageId(baseMessage.getId());
        }
        if (!baseMessage.getSenderId().isEmpty()){
            builder.setFromId(baseMessage.getSenderId());
        }
        if (!baseMessage.getRecipientId().isEmpty()){
            builder.setToId(baseMessage.getRecipientId());
        }
        if (baseMessage.getTimestamp() != 0){
            builder.setTimestamp(baseMessage.getTimestamp());
        }
        if (!baseMessage.getContent().isEmpty()){
            builder.setContent(baseMessage.getContent());
        }
        return builder.setMessageType(baseMessage.getType())
                .setMessageContentType(baseMessage.getContentType())
                .setStatusReport(baseMessage.getStatus())
                .build();
    }

    public static BaseMessage toBaseMessage(ChatMessageProtobuf.Msg msg){
        return new BaseMessage.Builder()
                .setId(msg.getMessageId())
                .setType(msg.getMessageType())
                .setContentType(msg.getMessageContentType())
                .setContent(msg.getContent())
                .setRecipientId(msg.getToId())
                .setSenderId(msg.getFromId())
                .setTimestamp(msg.getTimestamp())
                .setStatus(msg.getStatusReport())
                .build();
    }
}
